<?php

class nineDb
{
	private $link;
	private $config;
	
	function __construct($server, $user, $password, $dbname)
	{
		$this->config = array
		(
			'hostname'=>$server,
			'username'=>$user,
			'password'=>$password,
			'database'=>$dbname
		);
		
		$this->connect();
	}
	
	function __destruct()
	{
		$this->disconnect();
	}
	
	function connect()
	{
		$this->link = new mysqli(
			$this->config['hostname'],
			$this->config['username'],
			$this->config['password'],
			$this->config['database']
		);
	}
	
	function disconnect()
	{
		$this->link->close();
	}
	
	function query($sql)
	{
		return $this->link->query($sql);
	}
	
	function stringClean($string)
	{
		//todo:must detect and clean or block xss and injection attempts
		return($string);
	}
	
	function stringEscape($string)
	{
		if (get_magic_quotes_gpc()) {
			return($string);
		}
		else
		{
			return(addslashes($string));
		}
	}
	
	function numRows($sql)
	{
		$result = $this->query($sql);
		$numRows = $result->numRows();
		$result->close();
		return $numRows;
	}
	
	function numCols($sql)
	{
		$result = $this->query($sql);
		$numCols = $result->field_count();
		$result->close();
		return $numCols;
	}
	
	function getFields($sql)
	{
		//todo:this should return array or objects? :/
		$result = $this->query($sql);
		$getFields = $result->fetch_fields();
		$result->close();
		return $getFields;
	}

	function getObj($sql)
	{
		$result = $this->query($sql);
		while($row = $result->fetch_object())
		{
			$rows[] = $row;
		}
		$result->close();
		return $rows;
	}

	function getAll($sql)
	{
		$result = $this->query($sql);
		while($row = $result->fetch_array(MYSQLI_ASSOC))
		{
			$rows[] = $row;
		}
		$result->close();
		return $rows;
	}

	function getOne($sql)
	{
		$result = $this->query($sql);
		$row = $result->fetch_array(MYSQLI_NUM);
		$result->close();
		return $row[0];
	}
	
	function getRow($sql)
	{
		$result = $this->query($sql);
		$getRow = $result->fetch_array(MYSQLI_ASSOC);
		$result->close();
		return $getRow;
	}
	
	function getCol($sql)
	{
		die('Method is not complete');
	}
	
	function getInsertId()
	{
		return $this->link->insert_id;
	}
	
	function getAffectedRows()
	{
		return $this->link->affected_rows;
	}
	
	function autoExecute($tableName, $data, $action, $where=null)
	{
		if(strtolower($action)=='insert')
		{
			return $this->autoInsert($tableName, $data);
		}
		else if(strtolower($action)=='update')
		{
			return $this->autoUpdate($tableName, $data, $where);
		}
	}
	
	function autoInsert($tableName, $data)
	{
		foreach ($data as $field=>$value)
		{
			$fields[] = '`' . $field . '`';
			$values[] = '\'' . $this->stringEscape($value) . '\'';
		}
		$sql = 'INSERT INTO ' . $tableName . ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ');';
		$this->query($sql);
		return $this->getInsertId();
	}
	
	function autoUpdate($tableName, $data, $where)
	{
		foreach ($data as $field=>$value)
		{
			$pair[] = '`' . $field . '` = \'' . $this->stringEscape($value) . '\'';
		}
		$sql = 'UPDATE ' . $tableName . ' SET ' . implode(', ', $pair) . ' WHERE ' . $where . ';';
		$this->query($sql);
		return $this->getAffectedRows();
	}
 
}
?>